package com.system.healthproject.model.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.system.healthproject.bean.*;
import com.system.healthproject.mapper.PatientActivityMapper;
import com.system.healthproject.mapper.PatientActivitySignUpMapper;
import com.system.healthproject.model.admin.service.PatientActivityService;
import com.system.healthproject.pojo.PatientActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Xuheng Zhou
 * @date 2020/6/8 11:27
 * @desc
 */

@Service
public class PatientActivityServiceImpl implements PatientActivityService {

    @Autowired
    private PatientActivityMapper mapper;
    @Autowired
    private PatientActivitySignUpMapper signUpMapper;

    /**
     * 查询患教的条数
     * @param selectPatientActivityBean
     * @return
     */
    @Override
    public Integer selectPatientActivityCount(selectPatientActivityBean selectPatientActivityBean) {
        Integer page=selectPatientActivityBean.getPage();
        System.out.println("--"+page);
        //因为controller中先查询的所有数据，业务处理的时候，page已经减1改变了值，这里的page直接传入mapper进行查询
        Integer count= mapper.selectPatientActivityCount(selectPatientActivityBean);

        return count;
    }

    /**
     * 查询患教活动
     * @param selectPatientActivityBean
     * @return
     */
    @Override
    public List<PatientActivityResultBean> selectPatientActivity(selectPatientActivityBean selectPatientActivityBean) {
       Integer page=selectPatientActivityBean.getPage();
       Integer size=selectPatientActivityBean.getSize();
               page=(page-1)*size;
               selectPatientActivityBean.setPage(page);
        List<PatientActivityResultBean> list= mapper.selectPatientActivity(selectPatientActivityBean);
        for (PatientActivityResultBean p: list) {
            int signUpCount=selectSignUpPerson(p.getPatientactivityId());
            int watchPerson=selectWatchPerson(p.getPatientactivityId());
            double rate=(double) watchPerson/signUpCount;//观看率
            p.setViewRate(rate);

        }
        return list;
    }

    /**
     * 新增患教活动
     * @param patientActivityBean
     * @return
     */
    @Override
    public boolean insertPatientActivity(PatientActivityBean patientActivityBean) {
       //设置患教创建时间
        patientActivityBean.setPatientActivityCreateTime(new Date());

        return mapper.insertPatientActivity(patientActivityBean)>0?true:false;
    }

    /**
     * 根据id查询患教详情信息
     * @param patientactivityId
     * @return
     */
    @Override
    public PatientActivityOneBean selectPatientActivityById(Integer patientactivityId) {
        return mapper.selectPatientActivityById(patientactivityId);
    }

    /**
     * 取消患教活动
     * @param patientactivityId
     * @param cancelreason
     * @return
     */
    @Override
    public boolean cancelPatientActivity(Integer patientactivityId, String cancelreason) {
        return mapper.cancelPatientActivity(patientactivityId,cancelreason,new Date())>0?true:false;
    }

    /**
     * 更新编辑患教活动
     * @param updatePatientActivityOneBean
     * @return
     */
    @Override
    public boolean updatePatientActivity(UpdatePatientActivityOneBean updatePatientActivityOneBean) {
        return mapper.updatePatientActivity(updatePatientActivityOneBean)>0?true:false;
    }

    /**
     * 根据患教活动id查询报名患者信息
     * @param patientActivityId
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<PatientActivitySignUpBean> selectPatientActivitySignUp(Integer patientActivityId, Integer page, Integer size) {

        return signUpMapper.selectPatientActivitySignUp(patientActivityId,(page-1)*size,size);
    }

    /**
     * 查询患教报名的信息条数
     * @param patientActivityId
     * @param page
     * @param size
     * @return
     */
    @Override
    public Integer selectPatientActivitySignUpCount(Integer patientActivityId, Integer page, Integer size) {
        return signUpMapper.selectPatientActivitySignUpCount(patientActivityId,(page-1)*size,size);
    }

    /**
     * 根据医生id查询医生的患教活动
     * @param doctorId
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<selectPatientActivityByDoctorIdBean> selectDoctorPatientActivityByDoctorId(Integer doctorId, Integer page, Integer size) {

        List<selectPatientActivityByDoctorIdBean> list= mapper.selectDoctorPatientActivityByDoctorId(doctorId,(page-1)*size,size);
        for (selectPatientActivityByDoctorIdBean pd: list) {
            int signUpCount=selectSignUpPerson(pd.getPatientactivityId());
            pd.setSignUpPeopleNumber(signUpCount);
        }
        return list;
    }

    /**
     * 根据医生id查询医生的患教活动的条数
     * @param doctorId
     * @return
     */
    @Override
    public Integer selectPatientActivityCountByDoctorId(Integer doctorId) {
        return mapper.selectPatientActivityCountByDoctorId(doctorId);
    }

    @Override
    public boolean deletePatientActivityById(Integer patientactivityId) {
        return mapper.deleteByPrimaryKey(patientactivityId)>0?true:false;
    }

    /**
     * 根据医生id查询报名患教活动的观看人数
     * @param patientactivityId
     * @return
     */
    private Integer selectWatchPerson(Integer patientactivityId) {
        return signUpMapper.selectWatchPerson(patientactivityId);
    }

    /**
     * 根据患教id查询患教的报名人数
     * @param patientactivityId
     * @return
     */
    private Integer selectSignUpPerson(Integer patientactivityId) {
        return signUpMapper.selectSignUpPerson(patientactivityId);
    }



}
